package com.demo.core;

import com.demo.controller.model.RSResultMessage;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

public class I18nUtils {

    public final static String ZH = "zh";
    public final static String EN = "en";

    static Properties props = new Properties();
    static Properties props_en = new Properties();

    static {
        try {
            props.load(new InputStreamReader(I18nUtils.class.getResourceAsStream("/i18.properties"), "UTF-8"));
            props_en.load(new InputStreamReader(I18nUtils.class.getResourceAsStream("/i18_en.properties"), "UTF-8"));
//            System.out.println(props.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String getKey(String language, String key) {
        if (EN.equals(language))
            return props_en.getProperty(key, key);
        return props.getProperty(key, key);
    }

    public static RSResultMessage getErrorMessage(int errorcode) {
        String key = "ERROR" + String.valueOf(errorcode);
        RSResultMessage msg = new RSResultMessage();
        msg.setZh(props.getProperty(key, key));
        msg.setEn(props_en.getProperty(key, key));
        return msg;
    }

    private static int getLanguageIndex(String language) {
        if (EN.equals(language))
            return 1;
        return 0;
    }
}
